package com.qunar.flight.myserver;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptHandler implements Handler {

	private ServerSocketChannel channel;
	private Dispatcher dsp;

	AcceptHandler(ServerSocketChannel ssc, Dispatcher dsp) {
		channel = ssc;
		this.dsp = dsp;
	}

	public void handle() throws IOException {
		System.out.println(getClass());
		SocketChannel sc=channel.accept();//get The socket channel for the new connection
		sc.configureBlocking(false);
		Handler rh=new RequestHandler(sc);
		dsp.register(sc, SelectionKey.OP_READ, rh);//selector�ϰ�channel��channel���и���Ȥ���¼�,���и���Ȥ���¼�����ʱ���channel�Ϳ���ʹ����
	}
	
	public void run(){
		try {
			handle();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
